package cn.com.chengq.classloader;

import javafx.stage.Stage;

import java.applet.Applet;
import java.util.Arrays;

/**
 * 学习类加载器
 * 显示基本的类加载器
 */
public class AppMain {
    public static void main(String[] args) {
        for (Class clazz : Arrays.asList(Object.class, Stage.class, AppMain.class)) {
            ClassLoader classLoader = clazz.getClassLoader() != null ? clazz.getClassLoader() : null;
            String classLoaderName = classLoader != null ? classLoader.getClass().getName() : null;
            System.out.println(String.format("%s: %s", clazz.getName(), classLoaderName));
        }

        showClassLoader(AppMain.class);
        showClassLoader(int[].class);
        showClassLoader(Object[].class);
        showClassLoader(Stage[].class);
        showClassLoader(AppMain[].class);

    }

    public static void showClassLoader(Class clazz) {
        if (clazz == null) {
            return;
        }

        System.out.println(String.format("%s", clazz.getName()));

        ClassLoader classLoader = clazz.getClassLoader();
        while (classLoader != null) {
            System.out.println(String.format("    %s", classLoader.getClass().getName()));
            classLoader = classLoader.getParent();
        }
    }
}
